﻿using System;
using NLog;

namespace Aquarinum.WebSocket.Server.WinService
{
    public class ShellLogger
    {

        #region 单例封装

        private static ShellLogger _instance;
        private static readonly object Instancelocker = new object();


        private ShellLogger()
        {
            _logger = new LogFactory().GetCurrentClassLogger();
        }

        /// <summary>
        ///     单例实例
        /// </summary>
        /// <returns></returns>
        public static ShellLogger GetInstance()
        {
            //没有第一重 instance == null 的话，每一次有线程进入 GetInstance()时，均会执行锁定操作来实现线程同步，
            //非常耗费性能 增加第一重instance ==null 成立时的情况下执行一次锁定以实现线程同步
            if (_instance == null)
                lock (Instancelocker)
                {
                    //Double-Check Locking 双重检查锁定
                    if (_instance == null) _instance = new ShellLogger();
                }

            return _instance;
        }

        #endregion

        private readonly Logger _logger;

      

        #region 日志操作

        /// <summary>
        ///     Debug日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="tags"></param>
        public void Debug(string message, params string[] tags)
        {
            _logger?.Debug(message);
        }

        /// <summary>
        ///     记录错误日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="tags"></param>
        public virtual void Error(Exception ex, params string[] tags)
        {
            _logger?.Error(ex);
        }

        public void Info(string message, params string[] tags)
        {
            Log(message, LogLevel.Info, tags);
        }

        public void Trace(string message, params string[] tags)
        {
            Log(message, LogLevel.Trace, tags);
        }

        public void Warn(string message, params string[] tags)
        {
            Log(message, LogLevel.Warn, tags);
        }

        /// <summary>
        ///     记录Message日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="level"></param>
        /// <param name="tags"></param>
        public virtual void Log(string message, LogLevel level, params string[] tags)
        {
            _logger?.Log(level, message);
        }

        #endregion
    }
}